//
//  CameraViewController.m
//  GPUImage_MyDemo
//
//  Created by yuedongkui on 2018/5/24.
//  Copyright © 2018年 LY. All rights reserved.
//

#import "CameraViewController.h"
#import "GPUImage.h"

@interface CameraViewController ()

@property (nonatomic, strong) GPUImageVideoCamera *videoCamera;//注意，此处一定要写成属性，否则会报错
@property (nonatomic, strong) GPUImageStillCamera *stillCamera;

@property (nonatomic, strong) GPUImageFilter *customFilter;//滤镜
@property (nonatomic, strong) GPUImageView *filterView; //要展示的view

@property (nonatomic, copy) CameraPhotoBlock cameraPhotoBlock;


@end




@implementation CameraViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor greenColor];
    
    [self configGPUCommonSetting];
    [self configStillCameraWithPosition:AVCaptureDevicePositionBack];//静态图拍摄
//    [self configVideoCameraWithPosition:AVCaptureDevicePositionBack];//视频录制
        
    //关闭
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"meitu_embellish_beauty_icon_cancel"] forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonTap:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backButton];
    
    //切换前置摄像头
    UIButton *switchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [switchButton setImage:[UIImage imageNamed:@"live_ic_refresh"] forState:UIControlStateNormal];
    [self.view addSubview:switchButton];
    [switchButton addTarget:self action:@selector(switchButtonTap:) forControlEvents:UIControlEventTouchUpInside];
    [switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-20);
        make.top.equalTo(self.view).offset(100);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    //拍照
    UIButton *shootButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [shootButton setImage:[UIImage imageNamed:@"abc_btn_radio_to_on_mtrl_000"] forState:UIControlStateNormal];
    [shootButton addTarget:self action:@selector(shootButtonTap:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:shootButton];
    [shootButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-100);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
}

- (void)configGPUCommonSetting
{
    //设置滤镜
    self.customFilter = [[GPUImageFilter alloc] initWithFragmentShaderFromFile:@"CustomShader"];

    //设置展示画面的view
    self.filterView = [[GPUImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:self.filterView];
    
    //将滤镜和展示的view绑定
    [self.customFilter addTarget:self.filterView];
}

- (void)configStillCameraWithPosition:(AVCaptureDevicePosition)positon
{
    self.stillCamera = [[GPUImageStillCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:positon];
    self.stillCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    
    [self.stillCamera addTarget:self.customFilter];
    
    [self.stillCamera startCameraCapture];//开始捕捉画面
}

- (void)configVideoCameraWithPosition:(AVCaptureDevicePosition)positon
{
    self.videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset1280x720 cameraPosition:positon];
    self.videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
    
    [self.videoCamera addTarget:self.customFilter];
    
    [self.videoCamera startCameraCapture];//开始捕捉画面
}

- (void)captureImageCallback:(CameraPhotoBlock)block;
{
    _cameraPhotoBlock = block;
}

#pragma mark - buttons events
- (void)backButtonTap:(id)sender
{
    [self.stillCamera stopCameraCapture];
    [self.videoCamera stopCameraCapture];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)shootButtonTap:(id)sender
{
    if (self.stillCamera) //拍照
    {
        [self.stillCamera capturePhotoAsImageProcessedUpToFilter:self.customFilter
                                           withCompletionHandler:^(UIImage *processedImage, NSError *error)
         {
             NSData *dataForJPEGFile = UIImageJPEGRepresentation(processedImage, 0.8);
             UIImage *image = [UIImage imageWithData:dataForJPEGFile];

             if (self.cameraPhotoBlock) {
                 self.cameraPhotoBlock(image, error);
             }
             
             [self.stillCamera stopCameraCapture];//停止捕获画面
             self.filterView.hidden = YES;
             
             [self.navigationController popViewControllerAnimated:NO];
         }];
    }
    else if (self.videoCamera) //视频录制
    {
        //待研究...
    }
}

- (void)switchButtonTap:(id)sender
{
    if (self.stillCamera) {
        AVCaptureDevicePosition position = self.stillCamera.cameraPosition;
        if (position == AVCaptureDevicePositionFront) {
            [self configStillCameraWithPosition:AVCaptureDevicePositionBack];
        } else if (position == AVCaptureDevicePositionBack) {
            [self configStillCameraWithPosition:AVCaptureDevicePositionFront];
        }
    }
}

#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end



